<?php

/*
 * by jzp 747056764@qq.com
 */

namespace Account\Model;

use Common\Model\BaseModel;
use Common\Model\ModelException;
use Think\Log;
use Util\StringUtils;

class EmailModel extends BaseModel{
    
     protected $pk = 'email_id';
    protected $tableName = 'email';
    protected $token = 'email';
    protected $mailobj = null;
    
    
    
    public function sendMail($code,$email) {
            $content = "邮箱绑定验证码：$code";
            $config = D('Helper/Setting')->getByKey('site');
            $datas['sitename'] = $config['sitename'];
            $datas['tel'] = $config['tel'];
            $title = $config['sitename']."邮箱绑定";
            $content = "亲爱的".$config['sitename']."用户，你正在绑定邮箱操作，您的验证码为：".$code."，请尽快填写，以免认证超时！";
            if ($this->mailobj == null) {
            $mailconfig = D('Helper/Setting')->getByKey('mail');
            $mailconfig['sitename'] = $config['sitename'];
                $this->mailobj = $this->mail($mailconfig);
            }
            if (is_array($email)) {
                foreach ($email as $m) {
                    $this->mailobj->addAddress($m);
                }
                
            } else {
                $this->mailobj->addAddress($email);
            }
            $this->mailobj->Subject = $title;
            $this->mailobj->Body = $content;
            return $this->mailobj->send();
    }
    
    public function mail($config) {
        Vendor("phpmailer.PHPMailerAutoload");
        $mail = new \PHPMailer();
        $mail->isSMTP();
        $mail->Host = $config['smtp'];
        $mail->SMTPAuth = true;
        $mail->CharSet = "utf-8";
        $mail->Username = $config['username'];
        $mail->Password = $config['password'];
        $mail->SMTPSecure = 'ssl';
        $mail->Port = $config['port'];
        $mail->From = $config['from'];
        $mail->FromName = $config['sitename'];
        $mail->isHTML(true);
        return $mail;
    }
    
    public function getEorrer() {
        return $this->mailobj->ErrorInfo;
    }
}